package com.gmako.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.gmako.entity.Activityinfo;
import com.gmako.entity.Adress;
import com.gmako.entity.Businessimg;
import com.gmako.entity.Businessinfo;
import com.gmako.entity.Packageinfo;
import com.gmako.entity.Packimg;
import com.gmako.entity.Userinfo;
import com.gmako.service.ActivityinfoService;
import com.gmako.service.AddressService;
import com.gmako.service.BusinessinfoService;
import com.gmako.service.PackageinfoService;
import com.gmako.service.UserinfoService;
import com.gmako.utils.Constants;
import com.gmako.utils.Pager;
import com.gmako.utils.ResponseUtils;


@Controller
@RequestMapping("/business.do")
public class BusinessController {	
	@Autowired(required = false)
	private BusinessinfoService businessinfoService;
	@Autowired(required = false)
	private PackageinfoService packageinfoService;
	@Autowired(required = false)
	private ActivityinfoService activityinfoService;
	@Autowired(required = false)
	private UserinfoService userinfoService;


	
	/**
	 * 显示商家列表
	 * @author yezhenchi
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "method=view")
	public String viewLogin(@RequestParam("uid") String ruid,HttpServletRequest request){
		int uid = 0;
		int pageNo = 0;
		int pageSize = 3;
		if (ruid != null && ruid != "") {
			uid =Integer.parseInt(ruid.toString());
			System.out.println(uid);
		}
		Businessinfo bui = userinfoService.businessinfo(uid);
		int businessId = bui.getId();
		if (request.getParameter("pageNo") != null
				&& !"".equals(request.getParameter("pageNo"))) {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));

		} else {
			pageNo = Constants.pageNo;
		}
		Businessinfo binfo = businessinfoService.findBusinessInfoByBid(businessId);
		List<Businessinfo> businessinfolist1 = businessinfoService.findBusinessNameByType(Businessinfo.class,  "businessType", 1);
		List<Businessinfo> businessinfolist2 = businessinfoService.findBusinessNameByType(Businessinfo.class,  "businessType", 2);
		List<Businessinfo> businessinfolist3 = businessinfoService.findBusinessNameByType(Businessinfo.class,  "businessType", 3);
		List<Businessimg> bimg = businessinfoService.findBusinessImgs(businessId);
//		for(int i = 0;i<businessinfolist2.size();i++){
//			System.out.println("type=2"+businessinfolist2.get(i).getBusinessName());
//		}
		Pager p = businessinfoService.findPackInfoByBid(pageSize, pageNo,businessId);
		System.out.println("####显示商家列表");
		request.setAttribute("binfo",binfo);
		request.setAttribute("businessinfolist1",businessinfolist1);
		request.setAttribute("businessinfolist2",businessinfolist2);
		request.setAttribute("businessinfolist3",businessinfolist3);
		request.setAttribute("bz1",businessinfolist1.size());
		request.setAttribute("bz2",businessinfolist2.size());
		request.setAttribute("bz3",businessinfolist3.size());
		request.setAttribute("bimg", bimg);
		request.setAttribute("pager", p);
		request.setAttribute("businessId", businessId);
		return "businessShowPage";
	}	
	
	
	/**
	 * 显示商家列表分页
	 * @author yezhenchi
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "method=viewBusinessPager")
	public String viewBusinessPager(@RequestParam("pageNo") String r_pageNo,
			HttpServletRequest request, HttpServletResponse response)
	throws UnsupportedEncodingException {
		int businessId = 48;
		String sbusinessId = "";
		int pageNo = 1;
		int pageSize = 3;
		if (request.getParameter("bid") != null && request.getParameter("bid") != "") {
			businessId =Integer.parseInt(sbusinessId.toString());
		}
		if (request.getParameter("pageNo") != null && request.getParameter("pageNo") != "") {
			pageNo = Integer.parseInt(r_pageNo.toString());
		}
		Businessinfo binfo = businessinfoService.findBusinessInfoByBid(businessId);
		List<Businessinfo> businessinfolist1 = businessinfoService.findBusinessNameByType(Businessinfo.class,  "businessType", 1);
		List<Businessinfo> businessinfolist2 = businessinfoService.findBusinessNameByType(Businessinfo.class,  "businessType", 2);
		List<Businessinfo> businessinfolist3 = businessinfoService.findBusinessNameByType(Businessinfo.class,  "businessType", 3);
		List<Businessimg> bimg = businessinfoService.findBusinessImgs(businessId);
//		for(int i = 0;i<businessinfolist2.size();i++){
//			System.out.println("type=2"+businessinfolist2.get(i).getBusinessName());
//		}
		Pager p = businessinfoService.findPackInfoByBid(pageSize, pageNo,businessId);
		System.out.println("####显示商家列表");
		request.setAttribute("binfo",binfo);
		request.setAttribute("businessinfolist1",businessinfolist1);
		request.setAttribute("businessinfolist2",businessinfolist2);
		request.setAttribute("businessinfolist3",businessinfolist3);
		request.setAttribute("bz1",businessinfolist1.size());
		request.setAttribute("bz2",businessinfolist2.size());
		request.setAttribute("bz3",businessinfolist3.size());
		request.setAttribute("bimg", bimg);
		request.setAttribute("pager", p);
		return "businessShowPage";
	}
	/**
	 * 显示团购分页
	 * @author yezhenchi
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "method=viewTuangouPager")
	public String viewTuangouBusinessPager(
			@RequestParam("type") String r_type,
			HttpServletRequest request, HttpServletResponse response)
	throws UnsupportedEncodingException {
		String r_pageNo = request.getParameter("pageNo");
		int pageNo = 1;
		int type = 1;
		int pageSize = 9;
		if (r_pageNo != null && r_pageNo != "") {
			pageNo = Integer.parseInt(r_pageNo.toString());
		}
		if (request.getParameter("type") != null && request.getParameter("type") != "") {
			type = Integer.parseInt(r_type.toString());
		}
		Pager p = businessinfoService.findPagerHql(pageSize, pageNo,type);
		System.out.println(p.getResultList().size());
		request.setAttribute("pager", p);
		request.setAttribute("type",type);
		return "tuangouShowPage";
	}
	
	/**
	 * 跳转发起活动页面
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "method=jumpForActive")
	public String jumpForActive(HttpServletRequest request,@RequestParam("id") String id){
		Packageinfo p=packageinfoService.getPackageinfo(Integer.parseInt(id));
		Packimg img=packageinfoService.getPackimg(p.getId());
		Businessinfo binfo = businessinfoService.findBusinessInfoByBid(p.getBusinessInfoId());
		List<Businessinfo> businessinfolist1 = businessinfoService.findBusinessNameByType(Businessinfo.class,  "businessType", 1);
		List<Businessinfo> businessinfolist2 = businessinfoService.findBusinessNameByType(Businessinfo.class,  "businessType", 2);
		List<Businessinfo> businessinfolist3 = businessinfoService.findBusinessNameByType(Businessinfo.class,  "businessType", 3);
		request.setAttribute("businessinfolist1",businessinfolist1);
		request.setAttribute("businessinfolist2",businessinfolist2);
		request.setAttribute("businessinfolist3",businessinfolist3);
		request.setAttribute("bz1",businessinfolist1.size());
		request.setAttribute("bz2",businessinfolist2.size());
		request.setAttribute("bz3",businessinfolist3.size());
		request.setAttribute("binfo",binfo);
		request.setAttribute("img",img);
		request.setAttribute("p",p);
		return "addpackaactivityinfoe";
	}
	
	/**
	 * 保存发起活动
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "method=addActivity")
	public String addActivity(HttpServletRequest request,Activityinfo activityinfo){
		HttpSession session = request.getSession();
		Userinfo userinfo = (Userinfo)session.getAttribute("loginUser");
		activityinfo.setUserid(userinfo.getId());
		activityinfo.setAtype(0);//默认审核状态为0
		System.out.println(activityinfo.getActivityinfoName());
		activityinfoService.insert(activityinfo);
		return "";
	}
	
}
